package org.dev.app.enums;


import lombok.Getter;

import java.util.stream.Stream;

public enum ProjectTypeEnum {
	NA("-1", "未知"),
	FORMALPRO("1", "农业生产项目"),
	FORMALMACH("2", "农产品加工项目"),
	FREE("3", "非营利性公益项目"),
	FORMALDEV("4", "农业综合开发项目");

	@Getter
	private final String code;

	@Getter
	private final String name;

	ProjectTypeEnum(String code, String name) {
		this.code = code;
		this.name = name;
	}

	public static ProjectTypeEnum of(String code) {
		return Stream.of(values()).filter(r -> r.code.equals(code)).findFirst().orElse(NA);
	}

	public static ProjectTypeEnum from(String code) {
		return Stream.of(values()).filter(r -> r.code.equals(code)).findFirst()
				.orElseThrow(() -> new RuntimeException("未知枚举类型[" + code + "]"));
	}

	public static String toText(String code) {
		return of(code).getName();
	}

	public static boolean isNa(String code) {
		return of(code).equals(ProjectTypeEnum.NA);
	}

	public static ProjectTypeEnum ofName(String name){
		return Stream.of(values()).filter(r -> r.name.equals(name)).findFirst().orElse(NA);
	}

	public static String getKeyFromValue(String name){
		return ofName(name).getCode();
	}
}
